package com.bana.sys.controller;

import com.bana.sys.code.DatabaseResult;
import com.bana.sys.dao.UserDAO;
import com.bana.sys.model.*;

import com.bana.sys.service.UserService;
import com.bana.sys.utils.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value="/user" , method = RequestMethod.POST)
public class UserController {

    @Resource
    private UserDAO userDAO;
    @Resource
    private UserService userService;

    /**
     * 查询用户
     * @param request yhmc
     * @return
     */
    @RequestMapping(value = "/getUser.do", method = RequestMethod.POST)
    public User getUser(@RequestBody TokenRequest<User> request) {

        return userService.getUserWithFunctions(request.getData().getYhmc());

    }

    /**
     * 查询用户集合
     * @param request yhmc,分页
     * @return
     */
    @RequestMapping(value = "/getUsers.do", method = RequestMethod.POST)
    public PageResponse<User> getUsers(@RequestBody TokenRequest<UserPageParameter> request) {

        List<User> users=userService.getUsersWithFunctions(request.getData());
        int count=userDAO.getUserCount(request.getData());
        return new PageResponse<>(users,count);
    }


    /**
     * 新增用戶
     * @param request 用户
     * @return
     */
    @RequestMapping(value = "/insertUser.do",method = RequestMethod.POST)
    public ActionResult insertUser(@RequestBody TokenRequest<User> request){

        ActionResult actionResult = new ActionResult();
        int result = userService.insertUser(request.getData());
        actionResult.setCzjg(result);
        return actionResult;
    }
    /**
     * 更新用戶
     * @param request 用户
     * @return
     */
    @RequestMapping(value = "/updateUser.do",method = RequestMethod.POST)
    public ActionResult updateUser(@RequestBody TokenRequest<User> request){

        ActionResult actionResult = new ActionResult();
        User user=request.getData();
        userService.updateUser(user);
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    /**
     * 用户更新密码
     * @param request yhmc olddlmm dlmm
     * @return
     */
    @RequestMapping(value = "/updatePasswordByUser.do",method = RequestMethod.POST)
    public ActionResult updatePasswordByUser(@RequestBody TokenRequest<User> request) {

        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(userService.updatePasswordByUser(request.getData())?
                DatabaseResult.Success:
                DatabaseResult.Failed);
        return actionResult;
    }

    /**
     * 管理员更新密码
     * @param request yhmc,dlmm
     * @return
     */
    @RequestMapping(value = "/updatePasswordByAdmin.do",method = RequestMethod.POST)
    public ActionResult updatePasswordByAdmin(@RequestBody TokenRequest<User> request) {

        ActionResult actionResult=new ActionResult();
        userService.updatePasswordByAdmin(request.getData());
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    /**
     * 删除用戶
     * @param request yhmc
     * @return
     */
    @RequestMapping(value = "/deleteUser.do",method = RequestMethod.POST)
    public ActionResult deleteUser(@RequestBody TokenRequest<User> request) {

        ActionResult actionResult = new ActionResult();
        userService.deleteUser(request.getData().getYhmc());
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }
}
